<template>
  <div>
     <van-nav-bar
  title="注册"
  left-text="返回"
  
  left-arrow
  @click-left="onClickLeft"
 
/>
<div class="picture">
  <img src="@assets/photo.png" alt="">
</div>
<van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="userName"
    label="用户名"
    placeholder="用户名"
    :rules="[{ required: true, message: '请填写用户名' }]"
  />
  <van-field
    v-model="password"
    type="password"
    name="passWord"
    label="密码"
    placeholder="密码"
    :rules="[{ required: true, message: '请填写密码' }]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit">提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
//注册接口
import {addUser} from '@api'
// 提示框插件
import  {Toast}  from 'vant'
  export default {
  data() {
    return {
      username: '',
      password: '',
       forbidClick: true
    };
  },
  methods: {
    onSubmit(values) {
      Toast.loading({
        message:"注册中...",
         forbidClick: true
      })
      console.log('submit', values);
      addUser(values).then(res=>{
        if(res.data.code===201){
          ('注册成功')
          Toast.success("注册成功")
          this.forbidClick=false
        }else{
          if(res.data.code===403){
             Toast.fail("用户已存在")
          }else{
             Toast.fail("注册失败")
          }
          
        }
      })
      
    },
    onClickLeft(){
      this.$router.go(-1)
    }
  },
};
</script>

<style lang="scss" scoped>
.picture{
  width: 100%;
  overflow: hidden;
  padding: 60px 0;
  img{
    width: 40%;
  }
}
</style>